import typing

from fastapi import FastAPI, BackgroundTasks, Depends, Request
from typing import Annotated
from fastapi.middleware import Middleware
from datetime import datetime
import logging

app = FastAPI()

logging.basicConfig(filename="log.txt", level=logging.INFO)


@app.middleware("http")
async def request_log(request: Request, call_next):
    logging.info(f"Request: {request.method} {request.url.path}")
    return await call_next(request)


async def display_datetime(curr_datetime: datetime):
    print(curr_datetime)


class ParamChecker:
    def __call__(self, name: str):
        return None if name is None else name


param_checker = ParamChecker()


@app.get("/check")
async def API_check(in_param: Annotated[typing.Any, Depends(param_checker)]):
    return "Параметр отсутствует" if in_param in [None, ""] else {"name": in_param}


@app.get("/get_time")
async def API_get_time(bg_tasks: BackgroundTasks):
    curr_datetime = datetime.today()

    bg_tasks.add_task(display_datetime, curr_datetime)

    return {"current_datetime": curr_datetime}
